package com.browsesoft.user;

import java.sql.SQLException;
import java.util.Hashtable;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.browsesoft.ManagerGroup;
import com.browsesoft.SequenceFind;
import com.browsesoft.baseadapter.AdvancedSimpleEntity;
import com.browsesoft.dbtools.DBTools;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;

/**
 * 常用语
 */
public class SignSentence extends AdvancedSimpleEntity {
	/**
	 * 
	 */
	private static final long serialVersionUID = -6749774134074770926L;

	/**
	 * 存放属性值，常用语id，常用语内容和所对应的用户id
	 */
	private Hashtable attribute = new Hashtable();

	/**
	 * 空构造
	 */
	public SignSentence() {
	}

	/**
	 * 初始化model
	 * 
	 * @throws Exception
	 */
	public void init(HttpServletRequest request) throws Exception {
	}

	/**
	 * 设置签名属性
	 */
	public void setAttrbutes(String id, String content, User user) {
		this.attribute.put("id", id);
		this.attribute.put("content", content);
		this.attribute.put("userid", user.getID());
	}

	/**
	 * 插入
	 * 
	 * @throws Exception
	 */
	public void insert() throws Exception {
		this.getAttributes().put("id", Integer.toString(SequenceFind.getID()));
		try {
			DBTools.insert("signtable", this.getAttributes());
		} catch (SQLException e) {
			// 取消本次操作
			throw e;
		}
		SignSentencesManger manager = (SignSentencesManger) ManagerGroup
				.getManagerGroup().getManager("sign");
		manager.add(this);
	}

	/**
	 * 修改
	 * 
	 * @throws Exception
	 */
	public void update() throws Exception {
		try {
			DBTools.update("signtable",
					(String) this.getAttributes().get("id"), this.attribute);
		} catch (SQLException e) {
			throw e;
		}
	}

	/**
	 * 删除
	 * 
	 * @throws Exception
	 */
	public void delete() throws Exception {
		// 从库中删除
		try {
			DBTools
					.delete("signtable", (String) this.getAttributes()
							.get("id"));
		} catch (SQLException e) {
			// 取消本次操作
			throw e;
		}
		SignSentencesManger manager = (SignSentencesManger) ManagerGroup
				.getManagerGroup().getManager("sign");
		manager.remove((String) this.getAttributes().get("id"));
	}

	/**
	 * 得到id
	 */
	public String getID() {
		return null;
	}

	/**
	 * 服务请求
	 * 
	 * @param request
	 */
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		User user = (User) request.getSession().getAttribute("loginUser");
		String id = request.getParameter("parentid");
		// parentid 是判断增加删除的标志
		if (id != null) {
			this.getAttributes().put("userid", user.getID());
			this.insert();
		}
	}

	/**
	 * 更新属性
	 * 
	 * @param ht
	 *            存储属性的哈息表
	 */
	public void updateAttributes(Hashtable ht) throws Exception {
		this.getAttributes().remove("content");
		this.getAttributes().put("content", ht.get("content"));
		this.update();
	}

	/**
	 * 得到属性
	 * 
	 * @return 存储属性的哈息表
	 */
	public Hashtable getAttributes() {
		return this.attribute;
	}

	public Iterator getOptions(String name) throws Exception {
		return null;
	}

	/**
	 * 得到内容
	 * 
	 * @return
	 */
	public String getContent() {
		return (String) this.attribute.get("content");
	}

	/**
	 * 设置模型所对应的组件
	 * 
	 * @param component
	 *            对应的组件
	 */
	public void setComponent(HTMLBasicComponent component) {
	}

	/**
	 * 模型更新页面元素
	 * 
	 * @throws Exception
	 */
	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	/**
	 * 根据ID创建关系
	 * 
	 */
	public void createRelation() {
	}

	public void sychronDelete() {
		// TODO Auto-generated method stub

	}

	public void sychronUpdate(Hashtable attributes) {
		// TODO Auto-generated method stub

	}
}